//首页秒杀
'use strict';
import React,{Component} from 'react';
import styles from '../style/styles';
import Util from '../common/util'
import Icon from 'react-native-vector-icons/Ionicons';
import * as URLconstant from '../constant/URLconstant'
import SeckillIndex from '../marketing/seckill/seckillIndex';//秒杀详情
import SeckillList from '../marketing/seckill/list';//秒杀列表
import {
    View,
    Text,
    Image,
    ScrollView,
    TouchableOpacity
} from 'react-native';

export default class homeSecKill extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态
        this.state = {
            spreeList:[],
            timestamp:0
        };
        this.getListSuc=this.getListSuc.bind(this)
        this.errorCallback=this.errorCallback.bind(this)
    }


    componentDidMount() {
        this.getSpreeList()
        this.getTimeStamp()
    }

    componentWillUnmount() {
        this.timer&&clearInterval(this.timer)
    }

    getTimeStamp(){
        let that = this
        this.setState({
            timestamp:Date.parse(new Date())/1000
        })
        this.timer=setInterval(function () {
            let timestamp = Date.parse(new Date())/1000
            that.setState({
                timestamp:timestamp
            })
        },1000)
    }

    getSpreeList(){
        let url = URLconstant.GET_SPREE_LIST
        let datakey=[]
        datakey['user_id']=1
        Util.get(url,datakey,this.getListSuc,this.errorCallback)
    }
    getListSuc(datas){
        if(datas.status==0){
            let list = datas.data.list
            let spreeList = []
            for(let item of list){
                if(item.spree_status&&item.spree_status!=3){
                    spreeList.push(item)
                }
            }
            this.setState({
                spreeList:spreeList
            })
        }
    }
    errorCallback(err){

    }

    toSeckill(spree_id,goods_id){
        this.props.navigator.push({
            component:SeckillIndex,
            passProps:{
                spree_id:spree_id,
                goods_id:goods_id
            }
        })
    }

    toList(){
        this.props.navigator.push({
            component:SeckillList
        })
    }

    lastTime(time,status){
        let timestamp =this.state.timestamp
        let last_time
        if(status==1){
            last_time=timestamp-time
        }else{
            last_time=time-timestamp
        }
        let second = 0;
        let minute = 0;
        let hour = 0;
        let day = 0;
        if (last_time > 60) {
            second = parseInt(last_time % 60)
            minute = parseInt(last_time / 60)
            if (minute > 60) {
                hour = parseInt(minute / 60)
                minute = parseInt(minute % 60)
                if (hour > 24) {
                    day = parseInt(hour / 24)
                    hour = parseInt(hour % 24)
                }
            }
        } else {
            if (last_time > 0) {
                second = last_time
            } else {
                second = 0
            }
        }

        if (hour < 10) {
            hour = '0' + hour
        }
        if (minute < 10) {
            minute = '0' + minute
        }
        if (second < 10) {
            second = '0' + second
        }
        if (day > 0) {
            day = day + '天'
        } else {
            day = ''
        }
        return day + hour + ':' + minute + ':' + second
    }

    render() {
        let spreeList = this.state.spreeList
        return (
            <View>
                {
                    spreeList.length==0?
                        null:
                        <View style={{paddingTop:10,backgroundColor:"#fff"}}>
                            <View style={[styles.home_group_header,{marginBottom:0}]}>
                                <View style={{flexDirection:'row',alignItems:'center'}}>
                                    <Text style={{fontSize:18,color:"#000",marginLeft:10,}}>限时秒杀</Text>
                                    <Text style={{fontSize:12,color:"#000"}}>/flash SALE</Text>
                                </View>
                                <TouchableOpacity onPress={()=>this.toList()}>
                                    <Text style={{fontSize:12,color:"#969696"}}>更多></Text>
                                </TouchableOpacity>
                            </View>
                            {
                                spreeList.map((item,index)=>{
                                    if(index<3){
                                        return(
                                            <TouchableOpacity onPress={()=>this.toSeckill(item.spree_id,item.goods_id)} style={styles.home_spree_item} key={index}>
                                                <View style={styles.spree_txt_view}>
                                                    <Text style={{fontSize:14,color:"#666"}}>{item.spree_name}</Text>
                                                    <Text style={{fontSize:16,color:"#9e0a16",fontWeight:'bold'}}>秒杀价：¥{item.price}</Text>
                                                    <Text style={{fontSize:12,color:"#adadad",textDecorationLine:'line-through'}}>原价：¥{item.market_price}</Text>
                                                    {
                                                        item.spree_status==1?
                                                            <Text style={{fontSize:12,color:"#9e0a16"}}>距开始：{this.lastTime(item.begin_timestamp,item.spree_status)}</Text>:
                                                            <Text style={{fontSize:12,color:"#9e0a16"}}>距结束：{this.lastTime(item.end_timestamp,item.spree_status)}</Text>
                                                    }
                                                    <View style={{position:'absolute',right:10,bottom:10}}>
                                                        <View style={{marginRight:10,width:25,height:25,backgroundColor:"#9e0a16",borderRadius:12.5,alignItems:"center",justifyContent:"center"}}>
                                                            <Icon name={'md-cart'} size={20} color={'#ffffff'}/>
                                                        </View>
                                                    </View>
                                                </View>
                                                <Image
                                                    source={{uri:item.picture}}
                                                    resizeMode={'cover'}
                                                    style={{width:90,height:90}}
                                                />
                                            </TouchableOpacity>
                                        )
                                    }

                                })
                            }
                        </View>
                }
            </View>
        )
    }
}